package com.xcalendar.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xcalendar.constant.XConstants;
import com.xcalendar.database.dao.UserDAO;
import com.xcalendar.database.table.User;
import com.xcalendar.exception.IncorrectNameOrPasswordException;
import com.xcalendar.exception.NameUsedException;
import com.xcalendar.management.UserManager;
import com.xcalendar.managementImpl.UserManagerImpl;

@SuppressWarnings("serial")
public class CreateAccountController extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String login = request.getParameter("userName");
		String password = request.getParameter("password1");
/*		
		UserDAO userDao = new UserDAO();
		EventDAO eventDao = new EventDAO();
		LocationDAO locationDao = new LocationDAO();

		User newUser2 = new User();
		newUser2.setUserName(login);
		newUser2.setPassword(password);
		Key userKey2 = KeyFactory.createKey(User.class.getSimpleName(), newUser2.getUserName());
		newUser2.setKey(userKey2);
		

		Event newEvent = new Event();
		Date startTime = new Date(2011-1900,10,21,12,30,00);
		newEvent.setStartTime(startTime);
		Date endTime = new Date(2011-1900,10,21,13,30,00);
		newEvent.setEndTime(endTime);
		Date remindTime = new Date(2011-1900,10,21,12,00,00);
		newEvent.setRemindTime(remindTime);
		newEvent.setTitle("Lunch");
		newEvent.setDescription("Remeber to have lunch!");
		Event newEvent2 = new Event();
		Date startTime2 = new Date(2011-1900,10,21,18,30,00);
		newEvent2.setStartTime(startTime2);
		Date endTime2 = new Date(2011-1900,10,21,19,30,00);
		newEvent2.setEndTime(endTime2);
		Date remindTime2 = new Date(2011-1900,10,21,18,00,00);
		newEvent2.setRemindTime(remindTime2);
		newEvent2.setTitle("Dinner");
		newEvent2.setDescription("Remeber to have dinner!");
		newUser2.getEvents().add(newEvent);
		newUser2.getEvents().add(newEvent2);

		userDao.addUser(newUser2);
		
		User returnedUser2 = userDao.readUser(newUser2);
		System.out.println(returnedUser2.getPassword());
		HttpSession session = request.getSession(true);
		
		session.setAttribute(XConstants.SESSION_USER, returnedUser2);
		session.setAttribute(XConstants.ISLOGGED, "true");
		RequestDispatcher view = request.getRequestDispatcher("homepage_calendar.jsp");
	    view.forward(request, response);
*/		
	
		StringBuilder error = new StringBuilder();
		UserManager um = new UserManagerImpl();
		HttpSession session = request.getSession(true);
		try {
			um.createAccount(login, password);
		} catch (NameUsedException e) {
			error.append("User name already used. Try another one !");
		    request.setAttribute(XConstants.WRONY_MESSAGE, error.toString());
		    session.setAttribute(XConstants.ISLOGGED, "false");
		    RequestDispatcher view = request.getRequestDispatcher("register.jsp");
		    view.forward(request, response);
		    password = "";
		}
		
		User user = null;
		
		try {
			user = um.login(login, password);
			session.setAttribute(XConstants.SESSION_USER, user);
			session.setAttribute(XConstants.ISLOGGED, "true");
			RequestDispatcher view = request.getRequestDispatcher("homepage_calendar.jsp");
		    view.forward(request, response);
		} catch (IncorrectNameOrPasswordException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	    
	}
}
